package com.itheima.controller;

import com.itheima.dto.ProductDto;
import com.itheima.pojo.PmsProduct;
import com.itheima.service.PmsProductService;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.aggregation.ArrayOperators;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product")
public class PmsProductController {

    @Autowired
    private PmsProductService pmsProductService;

    /**
     * 查询商品
     */
    @GetMapping("/list")
    public ResponseEntity list(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               Integer brandId, String keyword, Integer productCategoryId,
                               String productSn, Integer publishStatus, Integer verifyStatus){

        PageResultVo<PmsProduct> prv = pmsProductService.list(pageNum,pageSize,brandId,
                                                                keyword,productCategoryId,productSn,
                                                                publishStatus,verifyStatus);
        return ResponseEntity.ok(Result.success(prv));
    }

    /**
     * 根据商品名称或货号模糊查询
     */
     @GetMapping("/simpleList")
    public ResponseEntity simpleList(String keyword){

        List<PmsProduct> list = pmsProductService.simpleList(keyword);

        return ResponseEntity.ok(Result.success(list));
     }

    /**
     * 批量修改删除状态
     */
    @PostMapping("/update/deleteStatus")
    public ResponseEntity deleteStatus(@RequestParam Integer deleteStatus,
                                       @RequestParam List<Integer> ids){

        pmsProductService.deleteStatus(deleteStatus,ids);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 批量设为新品
     */
    @PostMapping("/update/newStatus")
    public ResponseEntity newStatus(@RequestParam Integer newStatus,
                                    @RequestParam List<Integer> ids){

        pmsProductService.newStatus(newStatus,ids);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 批量上下架商品
     */
    @PostMapping("/update/publishStatus")
    public ResponseEntity publishStatus(@RequestParam Integer publishStatus,
                                        @RequestParam List<Integer> ids){

        pmsProductService.publishStatus(publishStatus,ids);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 批量推荐商品
     */
    @PostMapping("/update/recommendStatus")
    public ResponseEntity recommendStatus(@RequestParam Integer recommendStatus,
                                          @RequestParam List<Integer> ids){

        pmsProductService.recommendStatus(recommendStatus,ids);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 批量修改审核状态
     */
    @PostMapping("/update/verifyStatus")
    public ResponseEntity verifyStatus(@RequestParam Integer verifyStatus,
                                       @RequestParam List<Integer> ids){

        pmsProductService.verifyStatus(verifyStatus,ids);
        return ResponseEntity.ok(Result.success(null));
    }

    /**
     * 创建商品
     */
    @PostMapping("/create")
    public ResponseEntity create(@RequestBody ProductDto productParam){
        pmsProductService.create(productParam);
        return ResponseEntity.ok(Result.success(null));
    }


    /**
     *根据商品id获取商品编辑信息
     */
    @GetMapping("/updateInfo/{id}")
    public ResponseEntity updateInfo(@PathVariable Integer id){
        ProductDto productDto = pmsProductService.updateInfo(id);
        return ResponseEntity.ok(Result.success(productDto));
    }

    /**
     * 更新商品
     */
    @PostMapping("/update/{id}")
    public ResponseEntity updateProduct(@PathVariable Integer id,@RequestBody ProductDto productParam){
         pmsProductService.updateProduct(id,productParam);
         return ResponseEntity.ok(Result.success(null));
    }

}
